<?php
namespace app\decorate\model;
use app\BaseModel;
use think\facade\Db;



class AdminDynamic extends BaseModel
{
    //定义表名 动态表
    protected $name = 'longbing_card_decorate_dynamic';

    protected $append = [
      'site_text',
      'progress_text',
      'user_text' ,
      'area_text'
    ];

    static public function initModel(){
        return __CLASS__ ;
    }
    /**
     * @param $query
     * 查询范围
     */
    public function scopeStatus($query, $status){
        $query->where('a.status', '>', $status);
    }

    /**
     * User: chenniang
     * Date: 2019-10-30 13:49
     * @return void
     * descption:用户信息
     */
    public function getUserTextAttr($value,$data){
        $model = new AdminSite();
        $user  = $model->where(['id'=>$data['site_id']])->value('owner');
        $user  = Db::name('longbing_card_user')->where(['id'=>$user])->value('nickName');
        return $user;
    }

    /**
     * User: chenniang
     * Date: 2019-10-15 11:51
     * @param $value
     * @param $data
     * @return mixed
     * descption:获取地区名字
     */
    public function getAreaTextAttr($value,$data){
        $area_model = new AdminArea();
        $model = new AdminSite();
        $area  = $model->where(['id'=>$data['site_id']])->value('area');
        return $area_model->areaTitle(['id'=>$area]);
    }
    /**
     * User: chenniang
     * Date: 2019-10-23 16:24
     * @param $value
     * @param $data
     * @return mixed
     * descption:获取工地名称
     */
    public function getSiteTextAttr($value,$data){
        $model = new AdminSite();
        $site  = $model->siteTitle(['id'=>$data['site_id']]);
        return !empty($site)?$site:'工地已被删除';
    }
    /**
     * User: chenniang
     * Date: 2019-10-23 16:24
     * @param $value
     * @param $data
     * @return mixed
     * descption:获取进度名称
     */
    public function getProgressTextAttr($value,$data){
        $model = new AdminProgress();
        $progress =  $model->progressTitle(['id'=>$data['progress']]);
        return !empty($progress)?$progress:'进度已被删除';
    }

    /**
     * User: chenniang
     * Date: 2019-10-14 10:39
     * @param $value
     * @return void
     * descption:转换图片
     */
    public function getImgsAttr($value){
        $data = transImages([$value],['imgs']);
        return !empty($data[0])?$data[0]:[];
    }
    /**
     * User: chenniang
     * Date: 2019-10-14 10:39
     * @param $value
     * @return void
     * descption:转换视频
     */
    public function getVideoAttr($value){
        $data = transImagesOne([$value],['video']);
        return !empty($data[0])?$data[0]:[];
    }

    /**
     * User: chenniang
     * Date: 2019-10-24 14:19
     * @param $value
     * @return false|string
     * descption:转换时间
     */
    public function getCreateTimeAttr($value){
        return date('Y-m-d H:i',$value);
    }

    /**
     * @param $dis
     * @return array
     * @throws \think\exception\DbException
     * 动态列表
     */
    public function dynList($dis,$page = 10){
        $data = $this->alias('a')
            ->leftJoin('longbing_card_user_info b','b.fans_id=a.staff_id')
            ->leftJoin('longbing_card_job c','b.job_id = c.id')
            ->where($dis)
            ->status(-1)
            ->field(['a.*','b.name as staff_name','b.avatar','c.name as job_name'])
            ->group('a.id')
            ->order('create_time desc')
            ->paginate($page)
            ->toArray();
        $data = transImagesOne($data,['avatar']);
        return $data;
    }
    /**
     * User: chenniang
     * Date: 2019-10-23 14:24
     * @param $dis
     * @return array
     * descption:获取五条动态
     */
    public function dynSelect($dis){
        $data = $this
            ->alias('a')
            ->leftJoin('longbing_card_user_info b','a.staff_id = b.fans_id')
            ->where($dis)
            ->field('a.*,b.avatar')
            ->limit(5)
            ->select()
            ->toArray();
        $data = transImagesOne($data,['avatar']);
        return $data;
    }

    /**
     * @param $data
     * @return int|string
     * 添加动态
     */
    public function dynAdd($data){
        $data['create_time'] = time();
        $data['update_time'] = time();
        $data['status']      = 1;
        $data['video']       = !empty($data['video'])?$data['video']:0;
        $data['site_id']     = !empty($data['site_id'])?$data['site_id']:0;
        $data['progress']    = !empty($data['progress'])?$data['progress']:0;
        $res = $this->insert($data);
        return $res;
    }
    /**
     * @param $dis
     * @param $data
     * @return AdminCate
     * 编辑动态
     */
    public function dynUpdate($dis,$data){

        $data['update_time'] = time();

        $res = $this->where($dis)->update($data);

        return $res;
    }
    /**
     * @param $dis
     * @return AdminCate
     * 动态详情
     */

    public function dynInfo($dis){
        $data = $this->alias('a')
            ->leftJoin('longbing_card_user_info b','b.fans_id=a.staff_id')
            ->leftJoin('longbing_card_job c','b.job_id = c.id')
            ->where($dis)
            ->status(-1)
            ->field(['a.*','b.name as staff_name','b.avatar','c.name as job_name'])
            ->find();
        $data = !empty($data)?$data->toArray():$data;
        $data = transImagesOne($data,['avatar']);
        return $data;
    }




}